import 'dart:io';

import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/api.dart';
import 'clearance_list_widget.dart';
import 'package:gzmall_app/model/clearance_categories.dart';

class ClearanceListPage extends StatefulWidget {
  String title;

  ClearanceListPage(this.title);

  @override
  State<StatefulWidget> createState() => ClearanceListPageState(this.title);
}

class ClearanceListPageState extends State<ClearanceListPage>
    with TickerProviderStateMixin {
  String title;

  List<ClearanceCategories> _clearanceCategory = [];
  TabController _tabController;
  int _curIndex = 0;

  ClearanceListPageState(this.title);

  @override
  void initState() {
    super.initState();
    if (Platform.isIOS) {
      FlutterStatusbarManager.setStyle(StatusBarStyle.DARK_CONTENT);
    }

    _getShopCategoriesClearance();
  }

  @override
  void dispose() {
    if (Platform.isIOS) {
      FlutterStatusbarManager.setStyle(StatusBarStyle.LIGHT_CONTENT);
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (Platform.isIOS) {
      FlutterStatusbarManager.setStyle(StatusBarStyle.DARK_CONTENT);
    }
    _tabController = TabController(
        initialIndex: _curIndex,
        length: _clearanceCategory != null ? _clearanceCategory.length : 0,
        vsync: this);
    _tabController.addListener(() {
      _curIndex = _tabController.index;
    });
    return Theme(
        data: Theme.of(context).copyWith(
          primaryColor: Colors.white,
        ),
        child: Scaffold(
          appBar: _createAppBar(),
          body: Container(
            color: ColorCommon.grey5,
            child: new Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Container(
                    color: Colors.white,
                    margin: EdgeInsets.only(top: 1.0),
                    width: double.infinity,
                    height: 48.0,
                    child: new TabBar(
                      controller: _tabController,
                      isScrollable: true,
                      labelColor: ColorCommon.primaryColor,
                      unselectedLabelColor: ColorCommon.grey6,
                      tabs: _createClearanceCategoryView(),
                    ),
                  ),
                  new Expanded(
                    child: new TabBarView(
                      controller: _tabController,
                      children: _createClearanceViews(),
                    ),
                  ),
                ]),
          ),
        ));
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: new Text(
        this.title,
        style: TextStyle(color: Colors.black),
      ),
      leading: new IconButton(
        icon: new Icon(
          Icons.arrow_back_ios,
          color: Colors.black,
        ),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[],
    );
  }

  List<Widget> _createClearanceCategoryView() {
    List<Widget> _clearanceCategoryView = [];
    if (_clearanceCategory != null && _clearanceCategory.length > 0) {
      for (var c in _clearanceCategory) {
        _clearanceCategoryView.add(Tab(text: c.name));
      }
    }
    return _clearanceCategoryView;
  }

  List<Widget> _createClearanceViews() {
    List<Widget> _clearanceViews = [];
    if (_clearanceCategory != null && _clearanceCategory.length > 0) {
      for (var c in _clearanceCategory) {
        _clearanceViews.add(ClearanceListWidget(c.id));
      }
    }
    return _clearanceViews;
  }

  _getShopCategoriesClearance() {
    NetUtil.get(
      context,
      Api.shopCategoriesClearance,
      (data, message) {
        if (!mounted) return;
        setState(() {
          _clearanceCategory = (data as List)
              ?.map((e) => e == null ? null : ClearanceCategories.fromJson(e))
              ?.toList();
        });
      },
    );
  }
}
